<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1.事件冒泡</title>
  <style type="text/css">
    div {
      border: 1px solid #ccc;
      margin: 10px 0;
      padding: 1 10px;
    }
  </style>
</head>

<body>
  <button id="btn1">一个按钮</button>
  <div id="div1">
    <p id="p1">激活</p>
    <p id="p2">取消</p>
    <p id="p3">取消</p>
    <p id="p4">取消</p>
  </div>
  <div id="div2">
    <p id="p5">取消</p>
    <p id="p6">取消</p>
  </div>


  <script>
    let btn1 = document.getElementById('btn1')

    // 1.通用事件绑定函数
    function bindEvent(elem, type, fn) {
      elem.addEventListener(type, fn)
    }

    bindEvent(btn1, 'click', event => {
      // console.log(event.target);
      event.preventDefault() // 组织默认行为 例如链接跳转
      console.log('clicked');
    })

  // 2.事件冒泡
  const body = document.body
  const p1 = document.getElementById('p1')
  bindEvent(p1, 'click', event => {
    event.stopPropagation() // 阻止冒泡
    console.log('激活');
  })
  bindEvent(body, 'click', event => {
    // console.log('body click');
    console.log(event);
    console.log(event.currentTarget);
    console.log(event.target);
    console.log('取消');
  })

  // const div2 = document.getElementById('div2')
  // bindEvent(div2, 'click', event => {
  //   console.log('div2 click');
  //   console.log(event.target);
  // })





  </script>



</body>

</html>